package com.example.majiang.service;

import com.example.majiang.dto.PaginationDTO;
import com.example.majiang.dto.QuesitionDTO;
import com.example.majiang.exception.CustomizeErrorCode;
import com.example.majiang.exception.CustomizeException;
import com.example.majiang.mapper.QuestionMapper;
import com.example.majiang.mapper.UserMappper;
import com.example.majiang.model.Question;
import com.example.majiang.model.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private UserMappper userMapper;

    public PaginationDTO list(Integer page, Integer size) {

        PaginationDTO paginationDTO = new PaginationDTO();

        Integer totalPage;

        Integer totalCount = questionMapper.count();

        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        } else {
            totalPage = totalCount / size + 1;
        }

        if (page < 1) {
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }

        paginationDTO.setPagination(totalPage, page);
        //size*(page-1)
        Integer offset = size * (page - 1);
        List<Question> questions = questionMapper.list(offset, size);
        List<QuesitionDTO> questionDTOList = new ArrayList<>();

        for (Question question : questions) {
            User user = userMapper.findById(question.getCreator());
            QuesitionDTO questionDTO = new QuesitionDTO();
            BeanUtils.copyProperties(question, questionDTO);
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }

        paginationDTO.setData(questionDTOList);
        return paginationDTO;
    }

    public PaginationDTO list(Integer userId, Integer page, Integer size) {
        PaginationDTO paginationDTO = new PaginationDTO();

        Integer totalPage;

        Integer totalCount = questionMapper.countByUserId(userId);

        if (totalCount % size == 0) {
            totalPage = totalCount / size;
        } else {
            totalPage = totalCount / size + 1;
        }

        if (page < 1) {
            page = 1;
        }
        if (page > totalPage) {
            page = totalPage;
        }

        paginationDTO.setPagination(totalPage, page);
        List<Question> questions=null;

        if(page==0){
            Integer offset = size * page;
            questions = questionMapper.listByUserId(userId, offset, size);

        }
        else{
            Integer offset = size * (page - 1);
             questions = questionMapper.listByUserId(userId, offset, size);

        }

        //size*(page-1)
        List<QuesitionDTO> questionDTOList = new ArrayList<>();


        for (Question question : questions) {
            User user = userMapper.findById(question.getCreator());
            QuesitionDTO questionDTO = new QuesitionDTO();
            BeanUtils.copyProperties(question, questionDTO);
            questionDTO.setUser(user);
            questionDTOList.add(questionDTO);
        }

        paginationDTO.setData(questionDTOList);
        return paginationDTO;
    }
    public  QuesitionDTO getById(Integer id){
        Question question=questionMapper.getById(id);
        if(question==null){
            throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
        }
        QuesitionDTO questionDTO = new QuesitionDTO();
        BeanUtils.copyProperties(question, questionDTO);
        User user=userMapper.findById(question.getCreator());
        questionDTO.setUser(user);
        return questionDTO;
    }
    public void createOrUpdate(Question question){
        if(question.getId()==null){
            question.setGmtCreate(System.currentTimeMillis());
            question.setGmtModified(question.getGmtCreate());
            questionMapper.insert(question);
        }
        else{
            Question dbQuestion=questionMapper.getById(question.getId());
            if(dbQuestion==null){
                throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
            }
            question.setGmtModified(System.currentTimeMillis());
            questionMapper.update(question);
        }

    }
    public void incView(Integer id){
        Question question=new Question();
        question.setId(id);
        question.setViewCount(1);
        questionMapper.updateViewCount(question);
    }
    public List<QuesitionDTO> selectRelated(QuesitionDTO queryDTO){
        if (StringUtils.isBlank(queryDTO.getTag())) {
            return new ArrayList<>();
        }
        String[]tags=StringUtils.split(queryDTO.getTag(),",");
        String regexpTag = Arrays.stream(tags).collect(Collectors.joining("|"));
        Question question = new Question();
        question.setId(queryDTO.getId());
        question.setTag(regexpTag);
        List<Question>questionList=questionMapper.selectRelated(question);
        List<QuesitionDTO> questionDTOS = questionList.stream().map(q -> {
            QuesitionDTO questionDTO = new QuesitionDTO();
            BeanUtils.copyProperties(q, questionDTO);
            return questionDTO;
        }).collect(Collectors.toList());
        return questionDTOS;
    }

    public static void main(String[] args) {
        QuesitionDTO quesitionDTO=new QuesitionDTO();
        quesitionDTO.setTag("游戏王,龙魔导,红爹");
        quesitionDTO.setId(7);
        QuestionService questionService=new QuestionService();
        List<QuesitionDTO>quesitionDTOList=questionService.selectRelated(quesitionDTO);
        int i=0;
    }
}
